<?php
namespace Game\Controller;
use Common\Controller\CommonController;
class GameController extends CommonController{

    const AppID     =   'wx0dcb83c5892f258b';
    const AppSecret =   'aab64931415e6bb57986cb832518fc98';

    /**
     * @param $appid
     */
    public function lists()
    {
        $appid      =   I('post.appid','','htmlspecialchars');
        if (! empty($appid))
        {
            $data                       =   array();
            $data['bannerList']         =   array();
            $where_banner               =   array();
            $where_banner['headisshow'] =   1;
            $where_banner['h5show']     =   'false';
            $field_banner               =   array('headimg','appid','url');
            $banner_data                =   M('boxes_headapp')->where($where_banner)->field($field_banner)->limit(5)->select();
            if (! empty($banner_data))
            {
                //盒子appid wx6b1c7907c5733fc9
                $box_appid  =   'wx6b1c7907c5733fc9';

                foreach ($banner_data as $key=>$val)
                {
                    $banner             =   array();
                    $banner['appid']    =   $box_appid;
                    $banner['image']    =   'http://oss.beikg.cn/'.$val['headimg'];
                    $banner['params']   =   '';//'navigate=1&appid='.$box_appid.'&url='.$box_path;
                    $banner['path']     =   'pages/index/index?appid='.$val['appid'].'&page='.$val['url']; //$val['url'];
                    $banner['type']     =   '2';

                    $data['bannerList'][] = $banner;
                }
            }

            $where              =   array();
            $where['appid']     =   $appid;
            $where['status']    =   1;
            $field              =   array('id','name','breaf','logo','image','cover','url','shareImage','sharetitle','status');
            $data['gameList']   =   M('game_list')->where($where)->field($field)->select();
            $this->returnData($data,$msg='OK',$code=200);
        }
    }

    /**
     * @param $game_id
     * @param $appid
     */
    public function info()
    {
        $appid      =   I('post.appid','','htmlspecialchars');
        $game_id    =   I('post.game_id','','htmlspecialchars');
        if (! empty($appid) && ! empty($game_id))
        {
            $where              =   array();
            $where['appid']     =   $appid;
            $where['id']        =   $game_id;
            $field              =   array('id','name','breaf','logo','image','cover','url','shareImage','sharetitle','status');
            $data               =   M('game_list')->where($where)->field($field)->find();
            $this->returnData($data,$msg='OK',$code=200);
        }
    }

    /**
     * 游戏得分
     * @param $appid
     * @param $game_id
     * @param $score
     * @param $uid
     */
    public function score()
    {
        $appid      =   I('post.appid','','htmlspecialchars');
        $game_id    =   I('post.game_id','','htmlspecialchars');
        $score      =   I('post.score','','htmlspecialchars');
        $uid        =   I('post.uid','','htmlspecialchars');
        $data       =   array();

        if (! empty($appid) && ! empty($game_id)
        && ! empty($score) && ! empty($uid))
        {
            $where          =   array();
            $where['appid'] =   $appid;
            $where['uid']   =   $uid;
            $field          =   array('uid','nickname','avatarurl');
            $member_data    =   M('game_user')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                $insert_data                =   array();
                $insert_data['nickname']    =   $member_data['nickname'];
                $insert_data['avatarurl']   =   $member_data['avatarurl'];
                $insert_data['appid']       =   $appid;
                $insert_data['game_id']     =   $game_id;
                $insert_data['uid']         =   $uid;
                $insert_data['score']       =   $score;
                $insert_data['timestamp']   =   time();

                M('game_user_rank')->add($insert_data);

                $where_self             =   array();
                $where_self['appid']    =   $appid;
                $where_self['game_id']  =   $game_id;
                $where_self['uid']      =   $uid;
                $data['maxscore']       =   M('game_user_rank')->where($where_self)->max('score');

                $where_rank             =   array();
                $where_rank['appid']    =   $appid;
                $where_rank['game_id']  =   $game_id;

                $field                  =   array('avatarurl','nickname','uid','score');
                $rankList               =   M('game_user_rank')->where($where_rank)->field($field)->group('game_id,uid')->order('score desc')->limit(3)->select();
                $data['rankList']      =   array();

                if (! empty($rankList))
                {
                    foreach ($rankList as $key => $val)
                    {
                        $rank               =   array();
                        $rank['avatar']     =   $val['avatarurl'];
                        $rank['nickname']   =   $val['nickname'];
                        $rank['rank']       =   $key + 1;
                        $rank['score']      =   $val['score'];
                        $rank['userid']     =   $val['uid'];
                        $data['rankList'][] =   $rank;
                    }
                }

                $this->returnData($data,$msg='OK',$code=200);
            }
        }
    }

    /**
     * 排行榜
     * @param $appid
     * @param $game_id
     * @param $limit
     */
    public function rank()
    {
        $appid      =   I('post.appid','','htmlspecialchars');
        $game_id    =   I('post.game_id','','htmlspecialchars');
        $limit      =   I('post.limit','','htmlspecialchars');

        if (! empty($appid) && ! empty($game_id)
        && ! empty($limit))
        {
            $data                   =   array();
            $where_rank             =   array();
            $where_rank['appid']    =   $appid;
            $where_rank['game_id']  =   $game_id;
            $field                  =   array('avatarurl','nickname','uid','score');
            $rankList               =   M('game_user_rank')->where($where_rank)->field($field)->group('game_id,uid')->order('score desc')->limit($limit)->select();

            if (! empty($rankList))
            {
                foreach ($rankList as $key => $val)
                {
                    $rank               =   array();
                    $rank['avatar']     =   $val['avatarurl'];
                    $rank['nick']       =   $val['nickname'];
                    $rank['rank']       =   $key + 1;
                    $rank['sex']        =   0;
                    $rank['score']      =   $val['score'];
                    $rank['userid']     =   $val['uid'];
                    $data[]             =   $rank;
                }
            }

            $this->returnData($data,$msg='OK',$code=200);
        }
    }

    /**
     * 小组赛
     */
    public function group()
    {
        $data   =   array();
        $this->assign("data",$data);
        $this->display('group');
    }

    /**
     * 游戏配置
     */
    public function config()
    {
        $appid          =  	I('post.appid','','htmlspecialchars');
        $uid            =  	I('post.uid','','htmlspecialchars');
        $data           =   array();
        $data['title']  =   '原来@xxxx才是世界杯的幕后黑手？';
        $data['img']    =   'https://api.beikg.cn/Public/data/worldcuplast/share.jpg';
        if (! empty($appid) && ! empty($uid))
        {

            $table          =   'worldcuplast_member';
            $where          =   array();
            $where['uid']   =   $uid;
            $where['appid'] =   $appid;
            $field          =   array('uid','nickname');
            $result         =   M($table)->where($where)->field($field)->find();

            if (! empty($result))
            {
                $data['title']  =   "原来@{$result['nickname']}才是世界杯的幕后黑手？";
            }
        }
        $this->returnData($data,$msg='ok',$code=200);
    }
}